package be.kuleuven.mume.rest;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.json.JSONException;

import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;

import be.kuleuven.mume.JSONHelper;

@Path("/updates")
public class Updates {

	private static Logger log = Logger.getLogger(Updates.class.getName());
	
	@GET
	@Path("/")
	@Produces ("application/json")
	public String doGet(@QueryParam("rpp")@DefaultValue("10")int rpp, 
			@QueryParam("page")@DefaultValue("0")int page,
			@QueryParam("hashtag")@DefaultValue("*")String hashTag){
		try {
			log.log(Level.WARNING, "GET Updates", "rpp:" + rpp + " & page:" + page);
			
			Query query = new Query("Entry");
			query.addSort("datestamp", SortDirection.DESCENDING);
			if(!hashTag.equals("*"))
				query.addFilter("set", FilterOperator.EQUAL, hashTag);
			return JSONHelper.getQueryAsJSON(query, "Entry", rpp, page).toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return JSONHelper.ERROR;
	}
}
